import pandas as pd
from pickle import load
import plotly.graph_objects as go
import networkx as nx
import matplotlib.pyplot as plt
import math
RESULTS = "test_2"
iqp_results = load(open(f"./TEST_SETS/{RESULTS}_solution_iqp", "rb"))
heuristic_results = load(open(f"./TEST_SETS/{RESULTS}_solution_heuristic", "rb"))
# Getting IQP costs
iqp_trip = dict()
iqp_wait = dict()
iqp_cost = dict()
iqp_data = dict()
if 'stats' in iqp_results:
for a,r in iqp_results['stats'].items():
iqp_data.update({a: pd.DataFrame(r)})
iqp_trip.update({a: iqp_data[a]['COST TO REACH'].sum()})
iqp_wait.update({a: iqp_data[a]['WAIT_BEFORE'].sum()})
iqp_cost.update({a: iqp_data[a]['COST TO REACH'].sum() + iqp_data[a]['WAIT_BEFORE'].sum()})
# Getting HEURISTIC costs
heu_trip = dict()
heu_wait = dict()
heu_cost = dict()
heu_data = dict()
if 'stats' in heuristic_results:
for a,r in heuristic_results['stats'].items():
heu_data.update({a: pd.DataFrame(r)})
heu_trip.update({a: heu_data[a]['COST TO REACH'].sum()})
heu_wait.update({a: heu_data[a]['WAIT_BEFORE'].sum()})
heu_cost.update({a: heu_data[a]['COST TO REACH'].sum() + heu_data[a]['WAIT_BEFORE'].sum()})
# Plot traces
if len(iqp_data) > len(heu_data):
fig = go.Figure(data=[go.Bar(name="IQP_TRIPS",
x=[str(k) for k in iqp_trip.keys()]+['ALL'],
y=list(iqp_trip.values())+[sum(iqp_trip.values())],
offsetgroup=0),
go.Bar(name="IQP_WAITS",
x=[str(k) for k in iqp_wait.keys()]+['ALL'],
y=list(iqp_wait.values())+[sum(iqp_wait.values())],
offsetgroup=1),
go.Bar(name="HEURISTIC_TRIPS",
x=[str(k) for k in heu_trip.keys()]+['ALL'],
y=list(heu_trip.values())+[sum(heu_trip.values())],
offsetgroup=2),
go.Bar(name="HEURISTIC_WAITS",
x=[str(k) for k in heu_wait.keys()]+['ALL'],
y=list(heu_wait.values())+[sum(heu_wait.values())])],
offsetgroup=3)
else:
fig = go.Figure(data=[go.Bar(name="HEURISTIC_TRIPS",
x=[str(k) for k in heu_trip.keys()]+['ALL'],
y=list(heu_trip.values())+[sum(heu_trip.values())],
offsetgroup=0),
go.Bar(name="HEURISTIC_WAITS",
x=[str(k) for k in heu_wait.keys()]+['ALL'],
y=list(heu_wait.values())+[sum(heu_wait.values())],
offsetgroup=1),
go.Bar(name="IQP_TRIPS",
x=[str(k) for k in iqp_trip.keys()]+['ALL'],
y=list(iqp_trip.values())+[sum(iqp_trip.values())],
offsetgroup=2),
go.Bar(name="IQP_WAITS",
x=[str(k) for k in iqp_wait.keys()]+['ALL'],
y=list(iqp_wait.values())+[sum(iqp_wait.values())],
offsetgroup=3)])
# Change the bar mode and axis
fig.update_layout(barmode='group',
xaxis_title="Agent",
yaxis_title="Cost",)
fig.show()
# Plot traces
if len(iqp_data) > len(heu_data):
fig = go.Figure(data=[go.Bar(name="IQP",
x=[str(k) for k in iqp_cost.keys()]+['ALL'],
y=list(iqp_cost.values())+[sum(iqp_cost.values())]),
go.Bar(name="HEURISTIC",
x=[str(k) for k in heu_cost.keys()]+['ALL'],
y=list(heu_cost.values())+[sum(heu_cost.values())])])
else:
fig = go.Figure(data=[go.Bar(name="HEURISTIC",
x=[str(k) for k in heu_cost.keys()]+['ALL'],
y=list(heu_cost.values())+[sum(heu_cost.values())]),
go.Bar(name="IQP",
x=[str(k) for k in iqp_cost.keys()]+['ALL'],
y=list(iqp_cost.values())+[sum(iqp_cost.values())])])
# Change the bar mode and axis
fig.update_layout(barmode='group',
xaxis_title="Agent",
yaxis_title="Cost",)
fig.show()
# Plot traces
if len(iqp_data) > len(heu_data):
fig = go.Figure(data=[go.Bar(name="IQP",
x=["IQP"],
y=[iqp_results['time']]),
go.Bar(name="HEURISTIC",
x=["HEURISTIC"],
y=[heuristic_results['time']])])
else:
fig = go.Figure(data=[go.Bar(name="HEURISTIC",
x=["HEURISTIC"],
y=[heuristic_results['time']]),
go.Bar(name="IQP",
x=["IQP"],
y=[iqp_results['time']])])
# Change the bar mode and axis
fig.update_layout(barmode='group',
xaxis_title="Agent",
yaxis_title="Time(s)")
fig.update_yaxes(type="log")
fig.show()
def plot_model(list_of_trips, positions, lunch, limit=math.inf):
""" Function used to plot trips graph.
:param list_of_trips: List of edges,
:param positions: Nodes coordinates,
:param limit: Limit number of nodes,
"""
# Create figure
plt.figure(figsize=(16,9))
G = nx.Graph()
# Add nodes to graph
G.add_nodes_from({pk for pk in positions.keys() if pk < limit})
# Set labels dict
node_labels = dict()
# Set nodes positions
for n, p in positions.items():
if n < limit:
G.nodes[n]['pos'] = p
node_labels[n] = n
# Define edges colors
edge_colors = [plt.cm.tab20.colors[i] for i in range(len(list_of_trips))]
# Define edges labels list
edge_labels = dict()
# Add edges
for i,trip in enumerate(list_of_trips):
for j,t in enumerate(trip):
G.add_edge(t[0], t[1],
color=edge_colors[i],
alpha=0.5,
weight=4)
edge_labels.update({t: f"{j} (LUNCH)" if lunch[i][j+1] else j})
# Set edges parameters
edges = G.edges()
colors = [G[u][v]['color'] for u,v in edges]
weights = [G[u][v]['weight'] for u,v in edges]
alphas = [G[u][v]['alpha'] for u,v in edges]
# Draw Nodes
nx.draw_networkx_nodes(G, positions,
node_size=200,
node_color='c')
# Draw Labels
nx.draw_networkx_labels(G, positions,
node_labels,
font_size=10,
font_color='k')
# Draw Edges
nx.draw_networkx_edges(G, positions,
alpha=0.5,
width=weights,
edge_color=colors)
# Draw Edges Labels
nx.draw_networkx_edge_labels(G, positions,
edge_labels=edge_labels)
# Show graph
plt.show()
if 'domino' in iqp_results and 'positions' in iqp_results:
plot_model(iqp_results['domino'],
iqp_results['positions'],
[d['LUNCH_BEFORE'].astype(bool).to_list() for d in iqp_data.values()])
else:
print("No data available!")
if 'domino' in heuristic_results and 'positions' in heuristic_results:
plot_model(heuristic_results['domino'],
heuristic_results['positions'],
[d['LUNCH_BEFORE'].astype(bool).to_list() for d in heu_data.values()])
else:
print("No data available!")
# Plot results table
for a,d in iqp_data.items():
# Create figure
fig = go.Figure(data=[go.Table(
header=dict(values=list(d.columns),
fill_color='royalblue',
align='left',
font=dict(color='white', size=12),),
cells=dict(values=list(d.transpose().values),
fill_color='lavender',
align='left'))
])
# Set title
fig.update_layout(title=f"Agent {a}")
# Show figure
fig.show()
# Plot results table
for a,d in heu_data.items():
# Create figure
fig = go.Figure(data=[go.Table(
header=dict(values=list(d.columns),
fill_color='royalblue',
align='left',
font=dict(color='white', size=12),),
cells=dict(values=list(d.transpose().values),
fill_color='lavender',
align='left'))
])
# Set title
fig.update_layout(title=f"Agent {a}")
# Show figure
fig.show()